package com.ihrm.company.service.impl;


import com.ihrm.common.utils.IdWorker;
import com.ihrm.company.mapper.DepartmentMapper;
import com.ihrm.company.service.api.DepartmentService;
import com.ihrm.domain.company.Department;
import com.ihrm.domain.company.DepartmentExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author chentao
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private IdWorker idWorker;


    /**
     * 保存部门
     * 1.配置idwork到工程
     * 2.在service中注入idwork
     * 3.通过idwork生成id
     * 4.保存部门
     */
    @Override
    public void add(Department department) {
        /*得到id并且转化为String类型*/
        String id = idWorker.nextId() + "";
        department.setId(id);
        departmentMapper.insertSelective(department);
    }


    /**
     * 更新部门
     */
    @Override
    public void update(Department department) {
        departmentMapper.updateByPrimaryKeySelective(department);
    }


    /**
     * 根据id删除部门
     */
    @Override
    public void deleteById(String id) {
        departmentMapper.deleteByPrimaryKey(id);
    }


    /**
     * 根据id查询部门
     */
    @Override
    public Department findById(String id) {
        return departmentMapper.selectByPrimaryKey(id);
    }


    /**
     * 根据企业id查询部门列表
     */
    @Override
    public List<Department> findAll(String companyId) {
        DepartmentExample departmentExample = new DepartmentExample();
        DepartmentExample.Criteria criteria = departmentExample.createCriteria();
        criteria.andCompanyIdEqualTo(companyId);
        return departmentMapper.selectByExample(departmentExample);
    }

    @Override
    public Department findByCode(String code, String companyId) {
        return departmentMapper.findByCode(code, companyId);
    }
}
